﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DevelopTool.Bll.ChatBlls;
using DevelopTool.Common;
using DevelopTool.Model.ChatModels;
using DevelopTool.Model.Enums;
using ICSharpCode.TextEditor;

namespace DevelopTool.Forms.ChatForms.MessageItemControls
{
    public partial class MessageItem_Text : MessageItemBase
    {
        private ChatRecord chatRecord;

        private Action<ChatRecord> action;

        private ChatBll chatBll;

        public MessageItem_Text(ChatRecord chatRecord)
        {
            InitializeComponent();

            this.chatRecord = chatRecord;
        }

        private void MessageItem_Text_Load(object sender, EventArgs e)
        {
            this.chatBll = new ChatBll();

            this.richTextBox.Text = this.chatRecord.Text;

            if (chatRecord.SendUser.UserId == Setting.Chat.UserId)
            {
                this.richTextBox.RectColor = Setting.Theme.BackColor;
                this.richTextBox.FillColor = Setting.Theme.BackColor;
                this.richTextBox.FillColor2 = Setting.Theme.BackColor;
                this.richTextBox.ForeColor = Color.Black;
            }
            else
            {
                this.richTextBox.RectColor = Color.LightGray;
                this.richTextBox.FillColor = Color.LightGray;
                this.richTextBox.FillColor2 = Color.LightGray;
                this.richTextBox.ForeColor = Color.Black;
            }
        }


        public override void SizeChanged()
        {
            this.richTextBox.Dock = DockStyle.None;
            Size size = RichTextBoxHelper.ResizeUIRichTextBox(this.richTextBox, this.Parent.ClientSize);
            this.Size = size;
            this.richTextBox.Dock = DockStyle.Fill;

            base.SizeChanged();
        }


        public override void Send(Action<ChatRecord> action)
        {
            this.backgroundWorker_SendText.RunWorkerAsync(this.chatRecord);

            this.action = action;

            base.Send(action);
        }


        private void backgroundWorker_SendText_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            var chatRecord = (ChatRecord)e.Argument;

            try
            {
                chatRecord = chatBll.SendMessage(chatRecord, "TextMessage");
            }
            catch (Exception ex)
            {
                chatRecord.SendState = ChatMessageSendState.SendError;
                chatRecord.SendResult = ex.Message;
            }
            e.Result = chatRecord;
        }


        private void backgroundWorker_SendText_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {

        }

        private void backgroundWorker_SendText_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            var chatRecord = (ChatRecord)e.Result;

            this.chatBll.SaveChatRecord(chatRecord);

            this.action?.Invoke(chatRecord);
        }


        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(this.richTextBox.Text);
        }
    }
}
